#include <stdio.h>
#include<malloc.h>
#include<stdlib.h>

struct node
{
int value;
struct node *next;
}*first=NULL,*last=NULL,*temp;

//enter fn.
void enter(struct node *new_node)
{


int n,i;
new_node = malloc(sizeof(struct node));


printf("Enter Element");
scanf("%d",&n);

new_node->value = n;
new_node->next=NULL;
if (first==NULL||last==NULL)
        {
        first=new_node;
        last=new_node;
        }
else 
        {
        last->next=new_node;
        last=new_node;
        }


}

//extend fn
void extend()
{
enter(first); /*find better one */
}

//pop fn.
void pop(int opt)
{
     struct node *cur=first, *prev=NULL,*cur1=first;
     int opt2,i=0,j=0,n;
          if (opt==0)
     {
     while(cur1!=NULL)
     {
     cur1=cur1->next;
     j++;
     }
    opt2=j-1;
    while(i!=opt2)
        {
        prev=cur;
           cur=cur->next;
             i++;
           }
        prev->next=cur->next;
        free(cur);
        }  

     
   if(opt>0)
   {
        opt-=1;
        
        while(i!=opt)
        {
        prev=cur;
           cur=cur->next;
             i++;
           }
        prev->next=cur->next;
        free(cur);
        }  
                    
   
printf("Operation Successfull!!!");	  
	  
}

//apend fn.
void append()
{
     char f='y';
      printf("Enter elements in new list\n");
     while(f=='y')
     {
         enter(first);
     printf("Press y to enter more");
     scanf("%s",&f);
     }
 printf("list has been appended!");
 }
 
 //count fn.
void count()
{
struct node *q;
int c=0,n;
printf("Enter element to be counted");
scanf("%d",&n);
for(q=first;q!=NULL;q=q->next)
{
                              if (q->value==n)
                              c++;
}
printf("Count of %d is %d",n,c);
}


//index fn.
void ind()
{
     struct node *p;
     int n,in=0;
     printf("Enter element whose index no. is to be calculated");
     scanf("%d",&n);
     for(p=first;p!=NULL;p=p->next)
{
       in++;
       if (p->value==n)
       break;                             
}
 printf("Index no. of %d is %d",n,in-1);    
}

//insert fn.
void insert()
{
     int i=0,n,pos;
     struct node *cur=first,*val,*test;
     printf("Enter position where element has to be inserted");
     scanf("%d",&pos);
     printf("Enter the value");
     scanf("%d",&n);
     while(i!=pos-2)
     {
                  cur=cur->next;
                  
                  i++;
     }
 
    test=cur->next;    
    val=malloc(sizeof(struct node));
    val->value=n;
    val->next=test;
    cur->next=val;
    printf("Successfull");
   
     
}


//remove fn.
void rem()
{
     struct node *cur2=first, *prev=NULL;
     int n;
     printf("Enter value to be removed\n");
     scanf("%d",&n);
	  
 while(cur2->value!=n)
 {
          prev=cur2 ;
          cur2=cur2->next;
          }
          
  if (cur2 == NULL) 
    printf("value not in list");
  if (prev == NULL)
    first = first->next;       /* n is in the first node */
  else
    prev->next = cur2->next;  /* n is in some other node */
  free(cur2);
     
}

//sort fn.
void sort()
{
     struct node *t1,*t2,*t3;
  int temp;  
  for(t1=first;t1->next!=NULL;t1=t1->next)
  {
    for(t2=t1->next;t2!=NULL;t2=t2->next)
    {
      if(t2->value < t1->value)
      {
       temp=t1->value;
        t1->value=t2->value;
        t2->value=temp;   
      }
    }
  }
     
   printf("Operation Complete !!!!!");  
     
}


//reverse fn.
void reverse()
{ 
     struct node *temp=NULL, *f=first, *s=NULL;
while (f!=NULL)
        {
        temp=f->next;
        f->next=s;
        s=f;
        f=temp;
        }
first=last;
printf("Operation Completed !!!");

}

//display fn.
void display(struct node *new)
{
     printf("[");
while(new!=NULL)
{
printf("%d\t",new->value);
new=new->next;
}
printf("]");
}




//main
void main()
{

int a1[100],opt,opt2,opt3,i=0,num;
char ch;
do 
{
     system("cls");
printf("Enter your Choice number:");
printf(" \n0.Enter values \n1.Extend \n2.Pop \n3.Append \n4.Count \n5.Index \n6.Insert \n7.Remove \n8.Sort \n9.Reverse \n10.Display \n:>");  
scanf("%d",&opt);
switch(opt)
{
case 0:
do
{
 enter(first);
printf("Enter 1 to enter  values (enter any other no. to stop entering number):\t");
scanf("%d",&opt2);
}while(opt2==1); 

break;
case 1: extend(first);
break;
case 2: 
     printf("Enter position of no. to be deleted or enter 0 to delete from the end");
     scanf("%d",&opt3);
     pop(opt3);
break;
case 3: append();
break;
case 4: count();
break;
case 5: ind();
break;
case 6: insert();
break;
case 7: rem();
break;
case 8: sort();
break;
case 9: reverse();
break;
case 10: display(first);
break;
}
printf("\nPress 1. to continue");
scanf("%d",&opt2);
}while(opt2==1);
}
